package aad.storyboard.transitions;

import aad.storyboard.R;
import aad.storyboard.controller.Transition;
import android.app.Activity;
import android.os.Handler;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

public class SlideTransition extends Transition{

	private static ViewFlipper flipper;	
	private static Handler mHandler=new Handler();
	
	Animation flip_in_from_left;
	static Animation flip_in_from_right;
	static Animation flip_out_to_left;
	Animation flip_out_to_right;
	Activity activity;
	
		
	public SlideTransition (int time, long duration, ViewFlipper flip, Activity a)
	{	 
	 super(time, duration);
	 flipper = flip;
	 this.activity=a;
	 	  
	 
	 	flip_in_from_left = AnimationUtils.loadAnimation(a, R.anim.push_in_from_left);

		flip_in_from_right = AnimationUtils.loadAnimation(a, R.anim.push_in_from_right);

		flip_out_to_left = AnimationUtils.loadAnimation(a, R.anim.push_out_to_left);

		flip_out_to_right = AnimationUtils.loadAnimation(a, R.anim.push_out_to_right);
	}
	
	private Animation inFromRightAnimation(int duration) {
		
		Animation inFromRight = new TranslateAnimation(
		
		Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
		
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
		
		);
		
		inFromRight.setDuration(duration);
		
		inFromRight.setInterpolator(new AccelerateInterpolator());
		
		return inFromRight;
		
		}

	

	private static Animation outToLeftAnimation(int duration) {		
		Animation outtoLeft = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
		);
		outtoLeft.setDuration(duration);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	private Animation inFromLeftAnimation(int duration) {
		Animation inFromLeft = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
		);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	private Animation outToRightAnimation(int duration) {
		Animation outtoRight = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
		);
		outtoRight.setDuration(duration);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

	private Animation downFromTopAnimation(int duration)
	{
		
		Animation downFromTop= new TranslateAnimation(
			      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
			      Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
			  );
		downFromTop.setDuration(duration);
		downFromTop.setInterpolator(new AccelerateInterpolator());
		return downFromTop;
	}

	
	public static void applyTransition(RelativeLayout current, RelativeLayout next){		
		flipper.addView(current);
		flipper.addView(next);
	}   
	
	private Animation upFromBottomAnimation(int duration)
	{
		Animation upFromBottom= new TranslateAnimation(
			      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
			      Animation.RELATIVE_TO_SELF, +1.0f, Animation.RELATIVE_TO_SELF, 0.0f
			  );
		upFromBottom.setDuration(duration);
		upFromBottom.setInterpolator(new AccelerateInterpolator());
		return upFromBottom;
	}
	
		
	private static Runnable mFadeTimeTask = new Runnable() {
		   @Override
		public void run() {			   
			 flipper.showNext();	 
			 		   System.out.println("Flipping");
		   }
		};


	@Override
	public void execute() {
		flipper.setAnimation(outToLeftAnimation(500));
		//flipper.setOutAnimation(flip_out_to_left);
		mHandler.removeCallbacks(mFadeTimeTask);	    	
	    mHandler.postDelayed(mFadeTimeTask, 100);		
		
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		
	}

	
	
	}
